<template>
	<div class="role">
	  <el-form :inline="true" :model="roleSearch" label-width="70px" style="min-width: 1200px;" class='searchForm'>
		<el-form-item label="角色名称">
			<el-input v-model="roleSearch.name" placeholder="请输入" clearable/>
		</el-form-item>
		<el-form-item>
			<el-button type="primary" @click="reset">重置</el-button>
			<el-button type="primary" @click="search">搜索</el-button>
		</el-form-item>
	  </el-form>
	  <el-table :data="roles_list.results" class='table'>
		<el-table-column label="序号" width="70" type="index"/>
		<el-table-column label="角色名称" prop="name" min-width="100"/>
		<el-table-column label="关联用户个数" prop="users" min-width="120"/>
		<el-table-column label="关联权限个数" prop="permissions" min-width="120"/>
		<el-table-column label="创建人" prop="create_by_name"  min-width="150"/>
		<el-table-column label="更新人" prop="update_by_name"  min-width="150"/>
		<el-table-column label="创建时间" prop="create_time" sortable min-width="150"/>
		<el-table-column label="更新时间" prop="update_time" sortable min-width="150"/>
		<el-table-column align="center" min-width="180">
			<template #header>
			<el-button v-if='permission.has_add_permission' type='primary' @click="addRole">新增</el-button>
			</template>
			<template #default="scope">
			  <el-button v-if='permission.has_edit_permission' :icon="Edit" size="small" type='primary' @click="editRole(scope.row)">编辑</el-button
			  >
			  <el-button v-if='permission.has_delete_permission'
				@click="deleteRole(scope.row.id)"
				size="small"
				type="danger"
			  :icon="Delete"
				>删除</el-button
			  >
			</template>
		</el-table-column>
	  </el-table>
	  <div style="margin: 10px;">
	  	<el-pagination
	  	  v-model:current-page="page_size_params.page"
	  	  v-model:page-size="page_size_params.size"
	  	  :page-sizes="[10, 20, 30, 50]"
	  	  :small="small"
	  	  :disabled="disabled"
	  	  :background="background"
	  	  :hide-on-single-page="false"
	  	  layout="total, sizes, prev, pager, next, jumper"
	  	  :total="roles_list.count"
	  	  @size-change="handleSizeChange"
	  	  @current-change="handleCurrentChange"
	  	/>
	  </div>
	</div>
</template>

<script>
import {mapState, mapActions, mapGetters} from 'vuex'
import { ElMessage, ElMessageBox } from 'element-plus'
import { View, Delete, Edit, Plus, EditPen} from '@element-plus/icons-vue'

export default{
	computed: {
		...mapState(['pathPermission']),
		mainMenu: function() {
			return this.menu_list.filter((item)=>{
				return item.level === 1
			})
		}
	},
	data() {
		return {
			permission: '',
			roleSearch:{
				name: '',
			},
			page_size_params: {
				page: 1,
				size: 10,
			},
			count: 1,
			title: '编辑菜单权限',
			editDialogVisible: false,
			menu_level: [
				{name: '主菜单', id: 1},
				{name: '子菜单', id: 2}
				
			],
			roles_list: [],
			ViewVisible: false,
			menuSearch: {
				name: '',
				path: '',
				icon: '',
				parent: '',
				level:''
			},
			menuSave: {
				id: '',
				name: '',
				path: '',
				icon: '',
				parent: 0,
				level:''
			},
		}
	},
	setup() {
		return {
			Edit,
			Delete,
			Plus,
			View,
			EditPen,
		}
	},
	methods:{
		...mapActions(['getRolePermission']),
		handleCurrentChange(){
			this.getRoles()
		},
		handleSizeChange(){
			this.getRoles()
		},
		formatterLevel(row, column, cellValue, index){
			if (row.level === 1)
				return '主菜单'
			else{
				return '子菜单'
			}
		},
		search(){
			this.getRoles()
		},
		reset(){
			for(let key in this.roleSearch){
				this.roleSearch[key] = ''
			}
		},
		editRole(row_data){
			this.$router.push({path: '/user/roleEdit', query: {id: row_data.id}})
		},
		addRole(){
			this.$router.push({name: 'roleCreate'})
		},
		save(){
			this.editDialogVisible = false
			if (this.menuSave.id){
				this.updatePermission()
			}else{
				this.createPermission()
			}
		},
		async createPermission(params){
			const response = await this.$api.createPermission(this.menuSave)
			if(response.status === 201){
				this.getPermssions()
			}
		},
		async updatePermission(){
			const response = await this.$api.updatePermission(this.menuSave.id, this.menuSave)
			if(response.status === 200){
				this.menuSave = {}
				this.getPermssions()
			}
		},
		async deleteRole(id){
			const response = await this.$api.deleteRole(id)
			if (response.status === 204){
				this.getRoles()

			}
		},
		async getRoles(){
			const response = await this.$api.getRoles(Object.assign(this.roleSearch, this.page_size_params))
			if (response.status === 200){
				this.roles_list = {...response.data}
			}
		},
	},
	created(){
		console.log(this.pathPermission, 'path', this.$route.path)
		this.getRolePermission(this.pathPermission[this.$route.path]).then(res =>{
			this.permission = {...res.result}
		})
		this.getRoles()
	}
}
</script>

<style scoped>
	.role{
		width: 100%;
		display: flex;
		flex-direction: column;
		overflow: hidden;
	}
	.role .searchForm{
		width: 100%;
		flex: 1;
		background-color: white;
		padding: 15px 15px 0 15px;
		margin-bottom: 5px;
	}
	.role .el-form--inline .el-form-item{
		margin-bottom: 15px;
	}
	.role .table{
		background-color: white;
		flex: 1;
	}
	.searchForm .el-input{
		width: 150px;
	}
	.searchForm .el-select{
		width: 150px;
	}
	.form .el-select{
		width: 300px;
	}
	.el-button{
		width: 70px;
	}
	.el-icon{
		margin-left: 10px;
	}
	
</style>
